﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
namespace Yahoo.Yui.CompressorConsole
{
    public class ConfigHelper
    {
        XmlDocument xdoc = new XmlDocument();

        public ConfigHelper()
        {
            xdoc.Load("Yahoo.Yui.Compressor.config");
        }

        /// <summary>
        /// 读取配置文件的目录列表
        /// </summary>
        /// <returns></returns>
        public List<YuiDirectory> LoadDirectorys()
        {
            List<YuiDirectory> list = new List<YuiDirectory>();
            XmlNodeList xlist = xdoc.SelectNodes("/configuration/yui/directory");
            foreach (XmlNode xnode in xlist)
            {
                YuiDirectory dir = new YuiDirectory();
                if (xnode.Attributes["encoding"] != null)
                {
                    if (!string.IsNullOrEmpty(xnode.Attributes["encoding"].Value))
                    {
                        dir.Encoding = Encoding.GetEncoding(xnode.Attributes["encoding"].Value);
                    }
                    else
                    {
                        dir.Encoding = Encoding.GetEncoding("utf-8");
                    }
                }
                if (xnode.Attributes["ext"] != null)
                {
                    dir.Ext = xnode.Attributes["ext"].Value.ToLower();
                }
                if (xnode.Attributes["overwrite"] != null)
                {
                    dir.Overwrite = xnode.Attributes["overwrite"].Value.ToLower() == "true";
                }
                if (xnode.Attributes["path"] != null)
                {
                    dir.Path = xnode.Attributes["path"].Value;
                }
                if (xnode.Attributes["recursion"] != null)
                {
                    dir.Recursion = xnode.Attributes["recursion"].Value.ToLower() == "true";
                }
                dir.Exist = Directory.Exists(dir.Path);
                list.Add(dir);
            }
            return list;
        }

        /// <summary>
        /// 读取配置文件文件列表
        /// </summary>
        /// <returns></returns>
        public List<YuiFile> LoadFiles()
        {
            List<YuiFile> list = new List<YuiFile>();
            XmlNodeList xlist = xdoc.SelectNodes("/configuration/yui/file");
            foreach (XmlNode xnode in xlist)
            {
                YuiFile file = new YuiFile();
                if (xnode.Attributes["encoding"] != null)
                {
                    if (!string.IsNullOrEmpty(xnode.Attributes["encoding"].Value))
                    {
                        file.Encoding = Encoding.GetEncoding(xnode.Attributes["encoding"].Value);
                    }
                    else
                    {
                        file.Encoding = Encoding.GetEncoding("utf-8");
                    }
                }
                if (xnode.Attributes["ext"] != null)
                {
                    file.Ext = xnode.Attributes["ext"].Value.ToLower();
                }
                if (xnode.Attributes["overwrite"] != null)
                {
                    file.Overwrite = xnode.Attributes["overwrite"].Value.ToLower() == "true";
                }
                if (xnode.Attributes["path"] != null)
                {
                    file.Path = xnode.Attributes["path"].Value;
                }
                file.Exist = File.Exists(file.Path);
                list.Add(file);
            }
            return list;
        }

    }
}
